package com.zxcrm.web.controller.zxcrm;

import com.zxcrm.common.annotation.Log;
import com.zxcrm.common.core.controller.BaseController;
import com.zxcrm.common.core.domain.AjaxResult;
import com.zxcrm.common.core.page.TableDataInfo;
import com.zxcrm.common.enums.BusinessType;
import com.zxcrm.common.utils.poi.ExcelUtil;
import com.zxcrm.zxcrm.domain.ZxcrmCustomer;
import com.zxcrm.zxcrm.service.IZxcrmCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 客户Controller
 * 
 * @author xu
 * @date 2025-07-07
 */
@RestController
@RequestMapping("/zxcrm/customer")
public class ZxcrmCustomerController extends BaseController
{
    @Autowired
    private IZxcrmCustomerService zxcrmCustomerService;

    /**
     * 查询客户列表
     */
    @PreAuthorize("@ss.hasPermi('zxcrm:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZxcrmCustomer zxcrmCustomer)
    {
        startPage();
        List<ZxcrmCustomer> list = zxcrmCustomerService.selectZxcrmCustomerList(zxcrmCustomer);
        return getDataTable(list);
    }

    /**
     * 查询客户列表
     */
    @PreAuthorize("@ss.hasPermi('zxcrm:customer:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(ZxcrmCustomer zxcrmCustomer)
    {
        List<ZxcrmCustomer> list = zxcrmCustomerService.selectZxcrmCustomerList(zxcrmCustomer);
        return AjaxResult.success(list);
    }

    /**
     * 导出客户列表
     */
    @PreAuthorize("@ss.hasPermi('zxcrm:customer:export')")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZxcrmCustomer zxcrmCustomer)
    {
        List<ZxcrmCustomer> list = zxcrmCustomerService.selectZxcrmCustomerList(zxcrmCustomer);
        ExcelUtil<ZxcrmCustomer> util = new ExcelUtil<ZxcrmCustomer>(ZxcrmCustomer.class);
        util.exportExcel(response, list, "客户数据");
    }

    /**
     * 获取客户详细信息
     */
    @PreAuthorize("@ss.hasPermi('zxcrm:customer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(zxcrmCustomerService.selectZxcrmCustomerById(id));
    }

    /**
     * 新增客户
     */
    @PreAuthorize("@ss.hasPermi('zxcrm:customer:add')")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZxcrmCustomer zxcrmCustomer)
    {
        return toAjax(zxcrmCustomerService.insertZxcrmCustomer(zxcrmCustomer));
    }

    /**
     * 修改客户
     */
    @PreAuthorize("@ss.hasPermi('zxcrm:customer:edit')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZxcrmCustomer zxcrmCustomer)
    {
        return toAjax(zxcrmCustomerService.updateZxcrmCustomer(zxcrmCustomer));
    }

    /**
     * 成交客户
     */
    @PreAuthorize("@ss.hasPermi('zxcrm:customer:edit')")
    @Log(title = "客户成交", businessType = BusinessType.UPDATE)
    @PutMapping("/deal")
    public AjaxResult deal(@RequestBody ZxcrmCustomer zxcrmCustomer)
    {
        zxcrmCustomer.setDealTime(new Date());
        return toAjax(zxcrmCustomerService.updateZxcrmCustomer(zxcrmCustomer));
    }

    /**
     * 删除客户
     */
    @PreAuthorize("@ss.hasPermi('zxcrm:customer:remove')")
    @Log(title = "客户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zxcrmCustomerService.deleteZxcrmCustomerByIds(ids));
    }
}
